package playground.fibu.bean;

import java.util.ArrayList;
import java.util.List;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;

import playground.fibu.ejb.AccountBO;
import playground.fibu.ejb.dao.FibuDAOException;
import playground.fibu.ejb.entity.Account;

@ManagedBean
@SessionScoped
public class AccountBean {

	private AccountBO account;
	private List<AccountBO> accountList;
	
	public AccountBean(){
		account = new AccountBO();
		accountList = new ArrayList<AccountBO>();
		try {
			refreshData();
		} catch (Exception e) {
			// TODO: handle exception
		}
		
	}
	
	
	private void refreshData() throws FibuDAOException {
		accountList.clear();
		for (Account a : AccountBO.findAll()){
			accountList.add(new AccountBO(a));
		}		
	}


	public String save(){
		
		try {
			account.save();
			refreshData();
			
		} catch (Exception e) {
			System.out.println("FAAAILED");
			e.printStackTrace();
		}
		return null;
	}
	
	public String showFibu(){
		return "fibu";
	}


	public String getIban() {
		return account.getIban();
	}


	public void setIban(String iban) {
		account.setIban(iban);
	}


	public String getAccountName() {
		return account.getAccountName();
	}


	public void setAccountName(String accountName) {
		account.setAccountName(accountName);
	}


	public List<AccountBO> getAccountList() {
		return accountList;
	}
}
